$(document).ready(function(){
	var pId=$('#pId').val();
	initDatePickers();
	genDictSelect('scheduIntervalCode','timetype','1');
	initForm(type,pId);
	//事件绑定
	eventBind(type,pId);
	initHospitalSel(null,null,null);
});
//初始化form表单
function initForm(type,pId){
	$('#regForm').validation();
	//初始化所属医院级拉框
	initHospitalSel(null,null,null);
}
//事件绑定
function eventBind(type,pId){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	$('#save').on('click',function(){
		saveReg(pId);
	});
}
//查询医院
function initHospitalSel(defaultHosVal,defaultDeptsVal,defaultDocVal){
	$.getApiClient('/baseInfo/hospitals').get().done(function(data){
		if(!data.payload){
			data.payload=[];
		}
	    multipleSelectData("hospital",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), defaultHosVal, {valueAttr:'id', displayAttr:'hospital_name'},'请选择');
		$('#hospital').multipleSelect({width:'100%', single: true});
		
		//医院科室下拉框联动
		$('#hospital').change(function(){
			var hosid=$(this).children('option:selected').val();
			initDepartmentSel(hosid,defaultDeptsVal,defaultDocVal);
		}).change();
	});
}

//初始化所属科室下拉框
function initDepartmentSel(hosId,defaultVal,defaultDocVal){
	if(hosId){
		$.getApiClient('/baseInfo/departments').setQuery({hospital_id:hosId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("department",[{"id": "", "department_name": "请选择"}].concat(data.payload), defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#department').multipleSelect({width:'100%', single: true});
		});
		initAssignDocSel(defaultVal,defaultDocVal);
	}else{
		multipleSelectData("department",[{"id": "", "department_name": "请选择"}], defaultVal, {valueAttr:'id', displayAttr:'department_name'},"请选择");
		$('#department').multipleSelect({width:'100%', single: true});
		//科室医生下拉框联动
		$('#department').change(function(){
			var deptId=$(this).children('option:selected').val();
			initAssignDocSel(deptId,defaultDocVal);
		}).change();
	}
}

//初始化指定医生下拉框
function initAssignDocSel(deptId,defaultDocVal){
	if(deptId){
		$.getApiClient('/consultation/doctors/{department_id}').setQuery({department_id:deptId}).get().done(function(data){
			if(!data.payload){
				data.payload=[];
			}
		    multipleSelectData("user",[{"id": "", "user_name": "请选择"}].concat(data.payload), defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
			$('#user').multipleSelect({width:'100%', single: true});
		});
	}else{
		multipleSelectData("user",[{"id": "", "user_name": "请选择"}], defaultDocVal, {valueAttr:'id', displayAttr:'user_name'},"请选择");
		$('#user').multipleSelect({width:'100%', single: true});
	}
}

//挂号操作
function saveReg(pId){
	if(!$("#regForm").validationForm()){
		return;
	}
	$.getApiClient('/register/registerRemote').post($('#regForm').collect(), function(params){
		return $.extend(params, {
			hospital_id:$('#hospital').multipleSelect('getSelects','value')[0],
			department_id: $('#department').multipleSelect('getSelects','value')[0],
			assign_docid: $('#user').multipleSelect('getSelects','value')[0],
			use_time:$('#use_time').multipleSelect('getSelects','value')[0],
			timetype:$('#timetype').multipleSelect('getSelects','value')[0],
			patient_id:pId
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','远程预约成功',null,2000);
			pageQuery();
		}else{
			BootstrapDialog.closeAll();
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}
